﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using ProjectWebNhac.DTO;
using System.Data;


namespace ProjectWebNhac.GUI.Admin
{
    public partial class QL_BaiHat : System.Web.UI.Page
    {
        TheLoaiBus tlBus = new TheLoaiBus();
        BaiHatBus bhBus = new BaiHatBus();
        CaSiBus csbus = new CaSiBus();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadTheLoaiNhac();
                LoadGirdviewBaiHat(); 
            }
           
        }


        private void LoadGirdviewBaiHat()
        {
            dt = bhBus.LoadDanhSachBaiHat();
            gvDSBaiHat.DataSource = dt;
            gvDSBaiHat.DataBind();
        }

        private void LoadTheloainhacEdit()
        {
            drTheLoaiNhacEdit.DataSource = tlBus.LayTheLoaiNhac();
            drTheLoaiNhacEdit.DataValueField = "MaTheLoai";
            drTheLoaiNhacEdit.DataTextField = "TenTheLoai";
            drTheLoaiNhacEdit.DataBind();
        }

        protected void gvDSBaiHat_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Label lbCaSi = (Label)e.Row.FindControl("lbCaSi");
                Label lbTrangThai = (Label)e.Row.FindControl("lbTrangThai");
                LinkButton lbDetail = (LinkButton)e.Row.FindControl("lbDetail");
                LinkButton lnkXacNhan = (LinkButton)e.Row.FindControl("lnkXacNhan");
                int MaBH = Convert.ToInt16(lbDetail.Text);
                lbCaSi.Text = csbus.LayCaSiTheoBaiHat(MaBH);
                if(lbTrangThai.Text=="True")
                {
                    lbTrangThai.Text = "Xác Nhận";
                    lbTrangThai.CssClass = "badge badge-success";
                    lnkXacNhan.Visible = false;
                }
                else
                {
                    lbTrangThai.Text = "Chờ Xác Nhận";
                    lbTrangThai.CssClass = "badge badge-important";
                    lnkXacNhan.Visible = true;
                }
            }
        }

        protected void gvDSBaiHat_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ChiTiet"))
            {
                DisableAdd();
                int MaBH = Convert.ToInt16(e.CommandArgument);
                dt = bhBus.LoadChiTietBaiHat(MaBH);
                txtCaSi.Text = csbus.LayCaSiTheoBaiHat(MaBH);
                txtTenBaiHat.Text = dt.Rows[0]["TenBaiHat"].ToString();
                txtLyric.Text = dt.Rows[0]["Lyric"].ToString();
                txtLuotNghe.Text = dt.Rows[0]["LuotNghe"].ToString();
                drTheLoaiNhac.SelectedValue = dt.Rows[0]["MaTheLoai"].ToString();
                BaiHatDTO objBaiHat = new BaiHatDTO();
                objBaiHat.MaBaiHat = Convert.ToInt16(dt.Rows[0]["MaBaiHat"]);
                objBaiHat.TenBaiHat = txtTenBaiHat.Text;
                objBaiHat.TenCaSi = txtCaSi.Text;
                objBaiHat.Lyrics = txtLyric.Text;
                Session["CurrentBaiHat"] = objBaiHat;
                LoadTheloainhacEdit();
                divEdit.Visible = true;
                lnkThem.Visible = false;
                lnkHuy.Visible = true;
            }

            if (e.CommandName.Equals("Xóa"))
            {
                int MaBH = Convert.ToInt16(e.CommandArgument);
                bhBus.XoaBai(MaBH);
                LoadTheLoaiNhac();
                LoadGirdviewBaiHat(); 
            }

            if (e.CommandName.Equals("Xác Nhận"))
            {
                int MaBH = Convert.ToInt16(e.CommandArgument);
                bhBus.XacNhanBaiHat(MaBH);
                LoadTheLoaiNhac();
                LoadGirdviewBaiHat(); 
            }
        }

        private void EnableAdd()
        {
           
            txtCaSi.Enabled = true;
            txtCaSi.Text = "";
            
            txtLyric.Enabled = true;
            txtLyric.Text = "";
            txtTenBaiHat.Text = "";
            txtTenBaiHat.Enabled = true;
            drTheLoaiNhac.Enabled = true;
            flMusic.Enabled = true;
            lnkHuy.Visible = true;
            lnkThem.Text = "Lưu";
        }

        private void DisableAdd()
        {
            txtCaSi.Enabled = false;
            txtCaSi.Text = "";
            txtLyric.Enabled = false;
            txtLyric.Text = "";
            txtTenBaiHat.Text = "";
            txtTenBaiHat.Enabled = false;
            drTheLoaiNhac.Enabled = false;
            flMusic.Enabled = false;
            lnkHuy.Visible = false;
            lnkThem.Text = "Thêm";
            txtLuotNghe.Text = "";
        }

        protected void lnkThem_Click(object sender, EventArgs e)
        {
            if (lnkThem.Text.Equals("Thêm"))
            {
                EnableAdd();
            }
            else if (lnkThem.Text.Equals("Lưu"))
            {
                BaiHatDTO objBaiHat = new BaiHatDTO();
                objBaiHat.TenBaiHat = txtTenBaiHat.Text;
                objBaiHat.TenCaSi = txtCaSi.Text;
                objBaiHat.Lyrics = txtLyric.Text;
                objBaiHat.TaiKhoan = "admin";
                objBaiHat.TrangThai = 1;
                objBaiHat.MaTheLoai = Convert.ToInt16(drTheLoaiNhac.SelectedValue);
                objBaiHat.LinkBaiHat = flMusic.FileName;
                if (flMusic.PostedFile.FileName.Length>0)
                {
                    if (bhBus.ThemBaiHat(objBaiHat))
                    {
                        try
                        {
                            objBaiHat.LinkBaiHat = flMusic.PostedFile.FileName;
                            flMusic.SaveAs(Server.MapPath("~/Nhac/" + objBaiHat.LinkBaiHat));
                        }
                        catch (System.Exception ex)
                        {

                        }
                        DisableAdd();
                        LoadGirdviewBaiHat();
                    }
                    
                    
                } 
                else
                {
                }
            }

        }

        private void LoadTheLoaiNhac()
        {
            drTheLoaiNhac.DataSource = tlBus.LayTheLoaiNhac();
            drTheLoaiNhac.DataValueField = "MaTheLoai";
            drTheLoaiNhac.DataTextField = "TenTheLoai";
            drTheLoaiNhac.DataBind();
        }

        protected void lnkHuy_Click(object sender, EventArgs e)
        {
            LoadTheLoaiNhac();
            LoadGirdviewBaiHat();
            DisableAdd();
            lnkHuy.Visible = false;
            lnkThem.Visible = true;
            divEdit.Visible = false;
         }

        protected void btnSaveEdit_Click(object sender, EventArgs e)
        {
            BaiHatDTO objBaiHat = (BaiHatDTO)Session["CurrentBaiHat"];
            objBaiHat.TenCaSi = txtCasiEdit.Text;
            objBaiHat.TenBaiHat = txtTenBaiHatEdit.Text;
            objBaiHat.Lyrics = txtLyricEdit.Text;
            objBaiHat.MaTheLoai = Convert.ToInt16(drTheLoaiNhacEdit.SelectedValue);
            if (bhBus.SuaBaiHat(objBaiHat))
            {
                DisableAdd();
                LoadGirdviewBaiHat();
            }
        }


    }
}